export const commonMixin = {
  computed: {
    // 填充数据
    newTableData() {
      let t = this.tools.deepClone(this.tableData);
      if (t.length === 0) {
        return t;
      }
      while (t.length < 30) {
        t.push([]);
      }
      return t;
    },
    index() { // 序号计算
      return 15 * (this.currentPage - 1) + 1;
    },
    ids() {
      let t = _.cloneDeep(this.isCheckedList);
      let ids = [];
      t.forEach(item => {
        ids.push(item.id);
      });
      // console.log(ids)
      return ids;
    },
    list() {
      let t = _.cloneDeep(this.isCheckedList);
      let ids = [];
      t.forEach(item => {
        ids.push(item.id);
      });
      // console.log(ids)
      return ids.join(',');
    },
  },
  data() {
    return {
      // 表格加载
      tableLoading: false,
      // 表格数据
      tableData: [],
      // 选中的行
      isCheckedList: [],

      // 分页
      // 默认显示第几页
      currentPage: 1,
      // 总条数
      total: 0,
      // 个数选择
      pageSizes: [30, 20, 30, 40],
      // 默认显示每页显示条数
      pageSize: 30,
    };
  },
  methods: {
    // 清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 清除表单验证
    clearForm(formName) {
      this.$refs[formName].clearValidate();
    },

    // 表格类名
    getCellName(opt) {
      let { row } = opt;
      // console.log(row.length);
      if (row.length === 0) {
        return 'not-show';
      }
    },

    // 表格行点击事件，点击某一行也可以切换选择状态
    clickRow(row) {
      // console.log(row);
      // 如果当前行是空白，则不作反应
      if (row.length === 0) {
        return;
      }
      this.$refs.multipleTable.toggleRowSelection(row);
    },

    // 指定一个key标识这一行的数据，保持勾选状态
    getRowKey(row) {
      return row.id;
    },

    // val表示选中的表格行数据
    handleSelectionChange(val) {
      console.log(val);
    },

    // 判断当前行是不是空行，是否可点击
    selectInit(row) {
      // console.log(row);
      if (row.length === 0) {
        return false;
      } else {
        return true;
      }
    },

    // 表格勾选状态改变
    changeStatus(e) {
      this.isCheckedList = _.cloneDeep(e);
    },

    // 每页显示条数改变事件
    handleSizeChange(val) {
      // 改变每页默认显示条数
      this.pageSize = val;
      // 并且将页码显示到第一页
      this.currentPage = 1;
    },

    // 当前页改变时触发
    handleCurrentChange(val) {
      console.log(val);
      // 改变当前页数
      this.currentPage = val;
      this.getTable(val);
      this.$refs.multipleTable.bodyWrapper.scrollTop = 0;
    },

    // 清空表格，已经选中项
    clearTable(tableRef = 'multipleTable') {
      this.$refs[tableRef].clearSelection();
    }

  },
  created() {
  }
};

export const modalMixin = {
  methods: {
    $modal(options) {
      let {
        title,
        content,
        confirmHandle,
        cancelHandle = () => {
        }
      } = options;

      // console.log(cancelHandle);

      this.$confirm({
        centered: true,
        title: title,
        content: content,
        onOk: confirmHandle,
        onCancel: cancelHandle,
        cancelText: '取消',
        okText: '确定',
      });
    }
  },
};

export const deptMixin = {
  data() {
    return {
      departmentList: [],
      departmentOptions: [{
        label: '全部',
        value: ''
      }],
      addDepartmentOptions: [],
      key: 0,
    };
  },

  mounted() {
    setInterval(function() {
      document.querySelectorAll('.el-cascader-node__label').forEach(el => {
        el.onclick = function() {
          if (this.previousElementSibling) this.previousElementSibling.click();
        };
      });
    }, 1000);
  },

  methods: {
    // 获取科室信息
    async getDepartmentOptions(type) {
      if (this.departmentOptions.length > 1) {
        return;
      }
      try {
        const res = await this.$api.basics.getDepartmentList();
        if (res.data.type === 'success') {
          let { data } = res.data;
          this.getDeptData(data);
        }
      } catch (e) {
        this.departmentOptions = [];
      }
    },

    // 合并科室信息
    getDeptData(data) {
      data.forEach(item => {
        item.value = item.deptCode;
        item.label = item.deptName;
      });
      this.departmentList = _.cloneDeep(data);
      this.addDepartmentOptions = [
        ...this.tools.splitTree(_.cloneDeep(data), 'deptCode', 'parentDeptCode')
      ];
      this.departmentOptions = [
        { label: '全部', value: '' },
        ...this.tools.splitTree(_.cloneDeep(data), 'deptCode', 'parentDeptCode')
      ];
    },

    // 重置搜索表单
    resetSearchForm(formName) {
      console.log(1);
      this.resetForm(formName);
      this.currentPage = 1;
      this.getTable();
      this.clearTable('multipleTable');
    },
  },
};

export const pageMixin = {
  watch: {
    tableData: function(newVal, oldVal) {
      if (newVal.length <= 30 && this.total <= 30) {
        this.currentPage = 1;
      }
    },
  },
  data() {
    return {
      // 是否查询
      isSearch: false,
    };
  },
  methods: {
    // 设置查询确认
    setIsSearchTrue() {
      this.currentPage = 1;
      let param = this.getParam();
      // console.log(param);
      if (!_.isEmpty(param)) {
        this.isSearch = true;
      }
    },
    setIsSearchFalse() {
      this.isSearch = false;
    },

    // 删除之后判断当前页还几条
    afterDelete(res) {
      let { data } = res.data;
      let count = 0;
      for (let k in data) {
        if (data.hasOwnProperty(k)) {
          if (data[k] === true) {
            count++;
          }
        }
      }

      console.log(count);

      if (count === this.tableData.length) {
        // 设置为前一页
        this.currentPage = this.currentPage - 1;
        // 如果在第一页，就再设置为第一页
        this.currentPage <= 1 && (this.currentPage = 1);
      }

      this.getTable(this.currentPage);
      this.$refs.multipleTable.clearSelection();

    }

  },

};
